import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_uploader/flutter_uploader.dart';
import 'package:open_file/open_file.dart';
import '../../helper/Helper.dart';
import '../../helper/downloadCBH.dart';

import '../../helper/uidata.dart';
import '../../helper/upload_helper.dart';
import '../../model/upfile.dart';

class UpFilePanel extends StatefulWidget {
  final String spaceId;
  final String title;

  @override
  UpFilePanel(this.title, this.spaceId);
  @override
  UpFilePanelState createState() => new UpFilePanelState();
}

class UpFilePanelState extends State<UpFilePanel> {
  late List<UpFile> UpFileList;

  void getList() async {
    var _upList = await UploadHelper.getList();
    var _upL2 = _upList.where((p) => p.spaceId == widget.spaceId).toList();
    if (_upL2 == null) _upL2 = <UpFile>[];

    setState(() {
      UpFileList = _upL2;
    });
  }

  void addList(UpFile f1) async {
    if (f1 == null) return;

    if (this.mounted) {
      setState(() {
        UpFileList.add(f1);
      });
    }
  }

  // void setProgress(UploadTaskProgress progress) {
  //   for (var a in UpFileList) {
  //     if (a.taskId == progress.taskId) {
  //       if (this.mounted) {
  //         setState(() {
  //           a.progress = progress.progress / 100;
  //           if (progress.progress == 100) a.done = 100;
  //         });
  //       }
  //     }
  //   }
  // }

  void setValue(String id, int progress) async {
    for (var a in UpFileList) {
      if (a.taskId == id) {
        if (this.mounted) {
          setState(() {
            a.progress = progress / 100;
            if (progress == 100) a.done = 100;
          });
        }
      }
    }
  }

  void deleteFile(UpFile) {}

  Widget fileItem1(UpFile f1) {
    return ListTile(
        // leading: ImageWidget.UpFileImage(UpFileList[index].Name),
        //title: Text(f1.tag),
        title: Text(f1.baseName),
        trailing: Container(
          width: 10.0,
          height: 10.0,
          child: new CircleAvatar(
            backgroundColor: f1.done == 100 ? Colors.green : Colors.red,
            //radius: 2.0,
          ),
        ),
        onTap: () {
          OpenFile.open(f1.filePath);
        },
        onLongPress: () {
          print('11');
        });
  }

  Widget fileItem2(UpFile f1) {
    FileLoadProgress().on(f1.taskId, setValue);
    return ListTile(
        // leading: ImageWidget.UpFileImage(UpFileList[index].Name),
        // title: Text(f1.tag),
        title: Text(f1.baseName),
        trailing: CircularProgressIndicator(
          strokeWidth: 4.0,
          backgroundColor: Colors.red,
          value: f1.progress,
          valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
        ),
        onTap: () {
          OpenFile.open(f1.filePath);
        },
        onLongPress: () {
          print('11');
        });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getList();
    //  getProgress();
  }

  void upFile() async {
    File file = File((await Helper.getFilePickPath())!);
    var a1 = await UploadHelper.UpLoad11(
        file, "文件上传", UploadHelper.uuid.v1(), widget.spaceId);
    addList(a1!);
  }

  List<Widget> getWidgetList() {
    List<Widget> list1 = <Widget>[];
    list1.add(ListTile(
        title: Text(widget.title),
        trailing: ElevatedButton(
          child: Icon(Icons.add),
          onPressed: upFile,
        )));

    list1.add(Divider(
      height: 0.0,
    ));

    for (var a in UpFileList) {
      if (a.done == 100) {
        list1.add(fileItem1(a));
      } else {
        list1.add(fileItem2(a));
      }
      list1.add(Divider(
        height: 0.0,
      ));
    }

    return list1;
  }

  @override
  Widget build(BuildContext context) {
    // getList();

    if (UpFileList == null) return new Container();
    // TODO: implement build
    return Container(child: Column(children: getWidgetList()));
  }
}
